
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<span style="font-size:18px;">
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="weddingPhotography/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="weddingPhotography/css/commonly-used-show.css" />
    <script type="text/javascript" src="weddingPhotography/js/jquery.min.js"></script>
    <script type="text/javascript" src="weddingPhotography/js/bootstrap.js" ></script>

    <title>修改最新资讯</title>

</head>

<body class="mybody">

<jsp:include page="theTopMenuAdmin.jsp" />


<div class="container">
    <div class="frame" style="margin-bottom: 100px;">
        <h3 style="text-align: center">修改新闻</h3>
        <hr style="filter: alpha(opacity=100,finishopacity=0,style=3)" width="100%" color="#6f5999" size="3"/>

        <div style="margin-left: 10%; margin-right: 10%">

            <div class="form-group" style="float: left">
                <label for="new_name"><b style="color: red">*&nbsp</b>新闻标题：</label>
                <input type="text" id="new_name" class="form-control" style="width: 500px;" placeholder="请输入新闻标题" v-model="new_name">
            </div>


            <div class="form-group" style="float: left; margin-left: 50px">
                <label for="new_keyword">新闻关键字：</label>
                <input type="text" id="new_keyword" class="form-control edit" placeholder="请输入新闻关键字" v-model="new_keyword">
            </div>

            <div class="form-group" style="float: left">
                <label for="new_digest"><b style="color: red">*&nbsp</b>新闻摘要信息：</label>
               <textarea id="new_digest" style="height: 150px; width: 500px" class="form-control" placeholder="请输入新闻摘要信息" v-model="new_digest"></textarea>
            </div>

            <div class="form-group" style="float: left; margin-left: 50px">
                <label for="new_img"><b style="color: red">*&nbsp</b>新闻图片：</label>
                <div>
                    <img id="new_img" src="weddingPhotography/img/uploadly.png" width="290px" height="120px" >
                    <input type="file" onChange="selectImage(this)" style="width: 290px; margin-top: 10px">
                </div>

            </div>

            <div class="form-group">
                <label for="new_details"><b style="color: red">*&nbsp</b>新闻内容：</label>
               <textarea id="new_details" style="height: 350px;" class="form-control" placeholder="请输入新闻内容" v-model="new_details"></textarea>
            </div>

        </div>

        <div class="text-center" style="margin-top: 100px;">
            <div class="form-group">
                <input type="button" value="确认修改" onclick="present()" class="btn btn-primary">
            </div>

            <div>
                <label id="user_null" style="color: red"></label>
            </div>
        </div>

    </div>
</div>


<script type="text/javascript">
    var userdt = JSON.parse(sessionStorage.getItem("user"));
    var userNull = document.getElementById("user_null");
    var graphic;
    if (userdt.result.userName != null){
        $("#usname").text(userdt.result.userName);
    }else {
        $("#usname").text(userdt.result.userLoginName);
    }

    var newName;
    var newKeyword;
    var newDigest ;
    var newDetails;


    window.onload = function(){
        pnewsInfo();
    }

    function pnewsInfo(){
        var postUrl = "${pageContext.request.contextPath}/member/selectStripNews";

        var str = {
            "id": "${param.id}"
        };

        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(data){
                if (data.status == "0"){
                    alert(data.msg)
                }else {
                    var url = "${pageContext.request.contextPath}";
                    $("#new_name").val(data.result.newsTitle);
                    $("#new_keyword").val(data.result.newsKeywords);
                    $("#new_digest").val(data.result.newsDescription);
                    $("#new_img").attr("src", url + data.result.newsImage);
                    $("#new_details").val(data.result.newsContent);
                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }


    function present(){
        newName = $("#new_name").val();
        newKeyword = $("#new_keyword").val();
        newDigest = $("#new_digest").val();
        newDetails = $("#new_details").val();

        if ($.isEmptyObject(newName)){
            userNull.innerHTML = "新闻标题不能为空";
            return false;
        }else if ($.isEmptyObject(newDigest)){
            userNull.innerHTML = "新闻摘要不能为空";
            return false;
        }else if ($.isEmptyObject(newDetails)) {
            userNull.innerHTML = "新闻内容不能为空";
            return false;
        }else {
            userNull.innerHTML = "";
        }


        if ($.isEmptyObject(graphic)){
            mynewsly();
        }else {
            var postUrl = "${pageContext.request.contextPath}/newsUploadFile";
            var formData = new FormData();
            formData.append("newsType","newsimg");
            formData.append("pictureUrl",graphic);    //生成一对表单属性
            $.ajax({
                type: "post",           //因为是传输文件，所以必须是post
                url: postUrl,         //对应的后台处理类的地址
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.status == "0"){
                        userNull.innerHTML = data.msg;
                    }else {
                        mynews(data.result);
                    }
                },
                error:function(xhr,state,errorThrown){
                    //alert("发生网络错误，错误码为：" + xhr.status)
                    location.href = "${pageContext.request.contextPath}/error";
                }
            });
        }


    }

    function mynews(imgurl){
        if ($.isEmptyObject(imgurl)){
            userNull.innerHTML = "获取图片数据失败";
            return false;
        }else {
            userNull.innerHTML = "";
        }

        var postUrl = "${pageContext.request.contextPath}/member/updateNews";
        var str = {
            "id": "${param.id}",
            "newsTitle": newName,
            "newsKeywords": newKeyword,
            "newsDescription": newDigest,
            "newsContent": newDetails,
            "newsImage": imgurl.pictureUrl
        };

        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(data){
                if (data.status == "0"){
                    userNull.innerHTML = data.msg;
                }else {
                    location.href = "${pageContext.request.contextPath}/andminAllLatestNews";
                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }

    function mynewsly(){

        var postUrl = "${pageContext.request.contextPath}/member/updateNews";
        var str = {
            "id": "${param.id}",
            "newsTitle": newName,
            "newsKeywords": newKeyword,
            "newsDescription": newDigest,
            "newsContent": newDetails
        };

        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(data){
                if (data.status == "0"){
                    userNull.innerHTML = data.msg;
                }else {
                    location.href = "${pageContext.request.contextPath}/andminAllLatestNews";
                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }

    function selectImage(file) {
        if (!file.files || !file.files[0]) {
            return;
        }
        var reader = new FileReader();
        reader.onload = function (evt) {
            document.getElementById("new_img").src = evt.target.result;
        }
        //通过文件流将文件转换成Base64字符串
        reader.readAsDataURL(file.files[0]);
        graphic = file.files[0];
    }
</script>

</body>
</html>
</span>